# -*- coding: utf-8 -*-
'''
Created on 01-12-2012

@author: LONG HOANG GIANG
'''
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import pyLib
#from pyLib import html2text
from urlparse import urljoin
import datetime
import re
import workerpool
import json
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')

class Alobooks():
    
    xpathlst = ["//div[@class='content node-forum']/div[1]", "//div[contains(@class, 'content node')]/div[1]"]
    
    def getDetail(self, url, **kw):
        tree =  pyLib.loadWeb(url).build_tree()
        title = kw.get('title')
        for xpathstr in self.xpathlst:
            contentNode = tree.xpath(xpathstr)
            if len(contentNode) < 1: continue
            html = pyLib.Etree.tostring(contentNode[0])
            html = pyLib.getHTMLContent(html)
#            html = html2text.html2text(html, w100=True).strip()
#            html = pyLib.toUnicodeDungSan(html)
            html = pyLib.cleanHTML(html).strip()
            html = re.sub(r"(\s*\n\s*){3,}", "\n\n", html)
            html = re.sub(r"\n", "<br />", html)
            html = html.replace("previous page next page", '')
            if re.search(ur"(Chương[^<]+)", html):
                html = re.sub(ur"(Chương[^<]+)", "<b>\\1</b><br /><hr /><br />", html)
            else:
                html = "<b>{0}</b><br /><hr /><br />{1}".format(title, html)
            html = html.replace("<br /><br /> <br /><br />", "<br />")
            return html
        return None
    
    def normalizeTitle(self, title):
        return title
    
    def process(self, sUrl):
        
        outputPath = "/longhoanggiang/truyentext/{0}".format(pyLib.extractText(r"/([^/]+)\.html", sUrl, 1))
        logging.debug("output: {0}".format(outputPath))
        tree = pyLib.loadWeb(sUrl).build_tree()
        if tree == None: return
        data = [{"title": "GIỚI THIỆU", "url": sUrl}]
        
        for item in tree.xpath("//div[contains(@id, 'book-navigation')]/ul/li/a"):
            href = pyLib.normalizeStr(item.get('href', ''))
            if href == '': continue
            url = urljoin(sUrl, href)
            name = pyLib.toUpper(self.normalizeTitle(pyLib.stringify(item)))
            data.append({'title': name, 'url': url})
        
        def load(item):
            pyLib.loadWeb(item['url'])
            
        pool = workerpool.WorkerPool(size=5)
        pool.map(load, data)
        pool.shutdown()
        pool.wait()
        
        pyLib.createIfNotExistsPath(outputPath + "/files")
        
        chapters = []
        for item in data:
            detail = self.getDetail(item['url'], title=item['title'])
            chapterId = pyLib.crc32unsigned(item['url'])
            pyLib.gzip(outputPath + "/files/" + chapterId, json.dumps(detail))
            chapters.append({'name': item['title'], 'path': chapterId})
            print '______________________'
            print item['title']
            print '______________________'
            print detail, "\n\n"
        pyLib.gzip(outputPath + "/data", pyLib.encryptCipher(json.dumps(chapters)))
        return
   
   

def join(source, dest):
    
    source = '/longhoanggiang/truyentext/{0}'.format(source)
    dest = '/longhoanggiang/truyentext/{0}'.format(dest)
    
    
    dest_data = json.loads(pyLib.decryptCipher(pyLib.gz_file_get_content(dest + "/data")))
    src_data = json.loads(pyLib.decryptCipher(pyLib.gz_file_get_content(source + "/data")))
    
    for item in src_data:
        dest_data.append(item)
        
    pyLib.gzip(dest + "/data", pyLib.encryptCipher(json.dumps(dest_data)))
    
    for item in dest_data:
        print item['name']
    
    
    


if __name__ == '__main__':
    
    url = raw_input("enter url: ")
    logging.info("url: {0}".format(url))
#     join('hay-noi-yeu-thoi-t2', 'hay-noi-yeu-thoi')
    alobook = Alobooks()
    alobook.process(url)
    print '> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)